import {Component, OnInit, ViewEncapsulation, ChangeDetectorRef} from '@angular/core';
import {Subject} from 'rxjs';
import {ActivatedRoute, Router} from '@angular/router';
import {Location} from '@angular/common';

import {NewsService} from "@news/news.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {RecommendArticleVO} from "@interface/index";

@Component({
  selector: 'recommend',
  templateUrl: './recommend.component.html',
  styleUrls: ['./recommend.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class RecommendComponent implements OnInit {
  public recommendNews: Array<RecommendArticleVO>;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public cateId: number;
  public articleId: number;
  public rows: number;

  constructor(public router: Router,
              public route: ActivatedRoute,
              public newService: NewsService,
              public location: Location,
              public cdRef: ChangeDetectorRef,
              public tools: ToolsService) {
    this.route.params.subscribe(params => {
      this.cateId = params['cateId'];
      this.articleId = params['articleId'];
      this.getRecommendNews(params['cateId'], params['articleId']);

    });
  }

  /**
   * 获取推荐图集
   * @param cateId 资讯类型ID
   */
  getRecommendNews(cateId: number, articleId: number) {
    this.tools.showLoading();
    this.newService.getRecommendPhotoNews(cateId, articleId)
      .takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        this.tools.hideLoading();
        if (res.result === '0') {
          this.recommendNews = res.data;
          this.cdRef.detectChanges(); // 更新視圖
        }
      });
  }

  /**
   * 前往组图详情
   * @param news 推荐的组图
   */
  goToDetail(news: RecommendArticleVO) {
    this.router.navigate(['news/photo-news', news.cateId, news.articleId]);
  }

  ngOnInit() {

  }
}
